/*
 * CBRFormView.java
 */


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.*;
import javax.swing.*;
import jcolibri.datatypes.Instance;

/**
 * The application's main frame.
 */
public class CBRFormView extends JFrame {

    public CBRFormView() {
        
        this.app = new CBRApp();
        if (this.app.init() == false) {
         
          System.out.println("Failed to load! Exiting...");
          System.exit(0);
         
        }

        initComponents();
        
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        commonParameters = new javax.swing.JPanel();
        lblAtmosphericFronts = new javax.swing.JLabel();
        cmbAtmosphericFronts = new javax.swing.JComboBox();
        sldFronts = new javax.swing.JSlider();
        lblDayTime = new javax.swing.JLabel();
        cmbDayTime = new javax.swing.JComboBox();
        sldDayTime = new javax.swing.JSlider();
        lblHumidity = new javax.swing.JLabel();
        cmbHumidity = new javax.swing.JComboBox();
        sldHumidity = new javax.swing.JSlider();
        lblLocation = new javax.swing.JLabel();
        cmbLocation = new javax.swing.JComboBox();
        sldLocation = new javax.swing.JSlider();
        lblOvercast = new javax.swing.JLabel();
        cmbOvercast = new javax.swing.JComboBox();
        sldOvercast = new javax.swing.JSlider();
        lblPressureDynamic = new javax.swing.JLabel();
        cmbPressureDynamic = new javax.swing.JComboBox();
        sldPressureDynamic = new javax.swing.JSlider();
        lblTemperature = new javax.swing.JLabel();
        cmbTemperature = new javax.swing.JComboBox();
        sldTemperature = new javax.swing.JSlider();
        btnPredict = new javax.swing.JButton();
        tablePane = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();

        mainPanel.setName("mainPanel"); // NOI18N

        commonParameters.setBorder(javax.swing.BorderFactory.createTitledBorder("Common Parameters")); // NOI18N
        commonParameters.setName("commonParameters"); // NOI18N

        lblAtmosphericFronts.setText("Atmospheric Fronts"); // NOI18N
        lblAtmosphericFronts.setName("lblAtmosphericFronts"); // NOI18N

        cmbAtmosphericFronts.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cold Font", "Warm Front", "Cold and Warm Fronts" }));
        cmbAtmosphericFronts.setName("cmbAtmosphericFronts"); // NOI18N

        sldFronts.setPaintTicks(true);
        sldFronts.setValue(50);
        sldFronts.setName("sldFronts"); // NOI18N

        lblDayTime.setText("Time of day"); // NOI18N
        lblDayTime.setName("lblDayTime"); // NOI18N

        cmbDayTime.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Morning", "Day", "Evening", "Night" }));
        cmbDayTime.setName("cmbDayTime"); // NOI18N

        sldDayTime.setPaintTicks(true);
        sldDayTime.setName("sldDayTime"); // NOI18N

        lblHumidity.setText("Humidity"); // NOI18N
        lblHumidity.setName("lblHumidity"); // NOI18N

        cmbHumidity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "High", "Middle", "Low" }));
        cmbHumidity.setName("cmbHumidity"); // NOI18N

        sldHumidity.setPaintTicks(true);
        sldHumidity.setName("sldHumidity"); // NOI18N

        lblLocation.setText("Location"); // NOI18N
        lblLocation.setName("lblLocation"); // NOI18N

        cmbLocation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "America", "Belarus", "Russia", "Ukraine" }));
        cmbLocation.setName("cmbLocation"); // NOI18N

        sldLocation.setPaintTicks(true);
        sldLocation.setName("sldLocation"); // NOI18N

        lblOvercast.setText("Overcast"); // NOI18N
        lblOvercast.setName("lblOvercast"); // NOI18N

        cmbOvercast.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cirrus", "Layered", "Cumulus", "Rain" }));
        cmbOvercast.setName("cmbOvercast"); // NOI18N

        sldOvercast.setPaintTicks(true);
        sldOvercast.setName("sldOvercast"); // NOI18N

        lblPressureDynamic.setText("Dynamic of pressure"); // NOI18N
        lblPressureDynamic.setName("lblPressureDynamic"); // NOI18N

        cmbPressureDynamic.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Rise", "Fall", "Stable" }));
        cmbPressureDynamic.setName("cmbPressureDynamic"); // NOI18N

        sldPressureDynamic.setPaintTicks(true);
        sldPressureDynamic.setName("sldPressureDynamic"); // NOI18N

        lblTemperature.setText("Temperature"); // NOI18N
        lblTemperature.setName("lblTemperature"); // NOI18N

        cmbTemperature.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Low", "Middle", "High" }));
        cmbTemperature.setName("cmbTemperature"); // NOI18N

        sldTemperature.setPaintTicks(true);
        sldTemperature.setName("sldTemperature"); // NOI18N

        btnPredict.setText("Predict"); // NOI18N
        btnPredict.setName("btnPredict"); // NOI18N
        btnPredict.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPredictMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout commonParametersLayout = new javax.swing.GroupLayout(commonParameters);
        commonParameters.setLayout(commonParametersLayout);
        commonParametersLayout.setHorizontalGroup(
            commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(commonParametersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(commonParametersLayout.createSequentialGroup()
                        .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(commonParametersLayout.createSequentialGroup()
                                .addComponent(lblDayTime, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbDayTime, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(commonParametersLayout.createSequentialGroup()
                                .addComponent(lblAtmosphericFronts, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbAtmosphericFronts, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sldFronts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sldDayTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(commonParametersLayout.createSequentialGroup()
                        .addComponent(lblHumidity, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbHumidity, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sldHumidity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(commonParametersLayout.createSequentialGroup()
                        .addComponent(lblLocation, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbLocation, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sldLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(commonParametersLayout.createSequentialGroup()
                        .addComponent(lblOvercast, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbOvercast, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sldOvercast, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(commonParametersLayout.createSequentialGroup()
                        .addComponent(lblPressureDynamic, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbPressureDynamic, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sldPressureDynamic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(commonParametersLayout.createSequentialGroup()
                        .addComponent(lblTemperature, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbTemperature, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sldTemperature, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnPredict))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        commonParametersLayout.setVerticalGroup(
            commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(commonParametersLayout.createSequentialGroup()
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sldFronts, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblAtmosphericFronts)
                        .addComponent(cmbAtmosphericFronts)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldDayTime, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE)
                    .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblDayTime)
                        .addComponent(cmbDayTime)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldHumidity, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE)
                    .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblHumidity)
                        .addComponent(cmbHumidity)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblLocation)
                        .addComponent(cmbLocation))
                    .addComponent(sldLocation, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldOvercast, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE)
                    .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblOvercast)
                        .addComponent(cmbOvercast)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPressureDynamic)
                        .addComponent(cmbPressureDynamic))
                    .addComponent(sldPressureDynamic, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(commonParametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblTemperature)
                        .addComponent(cmbTemperature))
                    .addComponent(sldTemperature, javax.swing.GroupLayout.PREFERRED_SIZE, 20, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPredict))
        );

        tablePane.setName("tablePane"); // NOI18N

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Description", "Result", "Similarity"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        resultTable.setName("resultTable"); // NOI18N
        tablePane.setViewportView(resultTable);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tablePane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
                    .addComponent(commonParameters, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(commonParameters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tablePane, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        this.setContentPane(mainPanel);
        this.setSize(625,500);
        this.setVisible(true);
        
        this.addWindowListener(new WindowAdapter()
        {
         public void windowClosing(WindowEvent e)
         {
           dispose();
           System.exit(0); //calling the method is a must
           app.close();
         }
        });        
    }// </editor-fold>//GEN-END:initComponents

    // TODO: Произвести всю работу здесь
    private void btnPredictMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPredictMouseClicked
        // TODO add your handling code here:
        DefaultTableModel model = (DefaultTableModel)resultTable.getModel();
       
        model.setRowCount(0);
        resultTable.setDefaultRenderer(Object.class, new TableRenderer());
        
        String[][] conversions = { {"COLD_FRONT","WARM_FRONT","COLD_WARM_FRONTS"},
                                   {"MORNING","DAY","EVENING","NIGHT"},
                                   {"HIGH_HUMIDITY","MIDDLE_HUMIDITY","LOW_HUMIDITY"},
                                   {"AMERICA","BELARUS","RUSSIA","UKRAINE"},
                                   {"OVERCAST_CIRRUS","OVERCAST_LAYERED","OVERCAST_CUMULUS","OVERCAST_RAIN"},
                                   {"PRESSURE_RISE","PRESSURE_FALL","PRESSURE_STABLE"},
                                   {"LOW_TEMPERATURE","MIDDLE_TEMPERATURE","HIGH_TEMPERATURE"}
                                 };
        String []  query_params= new String[7];
        JComboBox[] boxes={
                           cmbAtmosphericFronts,
                           cmbDayTime,
                           cmbHumidity,
                           cmbLocation,
                           cmbOvercast,
                           cmbPressureDynamic,
                           cmbTemperature};
        
        JSlider[] sliders= {sldFronts,
                           sldDayTime,
                           sldHumidity,
                           sldLocation,
                           sldOvercast,
                           sldPressureDynamic,
                           sldTemperature};
        ArrayList<Double> weights= new  ArrayList<Double>();
        for (int i=0;i<sliders.length;i++) {
          weights.add((double)sliders[i].getValue()/(double)sliders[i].getMaximum());
        }
        ArrayList<String> instanceNames= new ArrayList<String>();
        for (int i=0;i<boxes.length;i++) {
            instanceNames.add(conversions[i][boxes[i].getSelectedIndex()]);
        }
        
        TwisterDescription descr = new TwisterDescription();
        try {
        descr.setAtmosphericFronts(new Instance(instanceNames.get(0)));
        descr.setDayTime(new Instance(instanceNames.get(1)));
        descr.setHumidity(new Instance(instanceNames.get(2)));
        descr.setLocation(new Instance(instanceNames.get(3)));
        descr.setOvercast(new Instance(instanceNames.get(4)));
        descr.setPressureDynamic(new Instance(instanceNames.get(5)));
        descr.setTemperature(new Instance(instanceNames.get(6)));
        } catch(jcolibri.exception.OntologyAccessException e) {
          e.printStackTrace();
        }
        ArrayList<UISolutionDescription> results = app.query(descr,weights);
        for(int i=0;i<results.size();i++)
        {
          Vector a = new Vector();
          a.add(results.get(i).description);
          a.add(results.get(i).result);
          Double s = new Double(results.get(i).similarity);
          a.add(s.toString());
          model.addRow(a);
        }
        
        
    }//GEN-LAST:event_btnPredictMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton btnPredict;
    public javax.swing.JComboBox cmbAtmosphericFronts;
    public javax.swing.JComboBox cmbDayTime;
    public javax.swing.JComboBox cmbHumidity;
    public javax.swing.JComboBox cmbLocation;
    public javax.swing.JComboBox cmbOvercast;
    public javax.swing.JComboBox cmbPressureDynamic;
    public javax.swing.JComboBox cmbTemperature;
    private javax.swing.JPanel commonParameters;
    private javax.swing.JLabel lblAtmosphericFronts;
    private javax.swing.JLabel lblDayTime;
    private javax.swing.JLabel lblHumidity;
    private javax.swing.JLabel lblLocation;
    private javax.swing.JLabel lblOvercast;
    private javax.swing.JLabel lblPressureDynamic;
    private javax.swing.JLabel lblTemperature;
    private javax.swing.JPanel mainPanel;
    public javax.swing.JTable resultTable;
    public javax.swing.JSlider sldDayTime;
    public javax.swing.JSlider sldFronts;
    public javax.swing.JSlider sldHumidity;
    public javax.swing.JSlider sldLocation;
    public javax.swing.JSlider sldOvercast;
    public javax.swing.JSlider sldPressureDynamic;
    public javax.swing.JSlider sldTemperature;
    private javax.swing.JScrollPane tablePane;
    
    
    public CBRApp app;
    // End of variables declaration//GEN-END:variables

    
}
